package cloud;

import agricultural.AgriculturalMachine;
import factory.AgriculturalMachineFactory;
import factory.MachineType;
import singleton.HighPrecisionNavigationSystem;

import java.util.ArrayList;
import java.util.List;

public class CloudManagementPlatform {
    private static CloudManagementPlatform instance;
    private List<AgriculturalMachine> machines;
    private HighPrecisionNavigationSystem navigationSystem;
    private List<String> tasks; // 作业任务列表

    private CloudManagementPlatform() {
        machines = new ArrayList<>();
        navigationSystem = HighPrecisionNavigationSystem.getInstance();
        tasks = new ArrayList<>();
        System.out.println("云端管理平台初始化完成");
    }

    public static synchronized CloudManagementPlatform getInstance() {
        if (instance == null) {
            instance = new CloudManagementPlatform();
        }
        return instance;
    }


    public AgriculturalMachine createAgriculturalMachine(MachineType type, String machineId) {
        AgriculturalMachine machine = AgriculturalMachineFactory.createMachine(type, machineId);
        machines.add(machine);
        System.out.println("云端管理平台创建了新的农机设备: " + machineId + " (" + type + ")");
        return machine;
    }

    public void assignTask(String area, String taskType, double workload) {
        String task = "在" + area + "执行" + taskType + "任务，工作量" + workload + "公顷";
        tasks.add(task);
        System.out.println("云端管理平台分配任务: " + task);

        // 模拟任务分配优化
        if ("播种".equals(taskType)) {
            for (AgriculturalMachine machine : machines) {
                if (machine instanceof agricultural.Planter) {
                    System.out.println("分配任务给: " + machine.getMachineId());
                    machine.setOperationStatus("作业中");
                    break;
                }
            }
        } else if ("收割".equals(taskType)) {
            for (AgriculturalMachine machine : machines) {
                if (machine instanceof agricultural.Harvester) {
                    System.out.println("分配任务给: " + machine.getMachineId());
                    machine.setOperationStatus("作业中");
                    break;
                }
            }
        } else if ("耕地".equals(taskType)) {
            for (AgriculturalMachine machine : machines) {
                if (machine instanceof agricultural.Tractor) {
                    System.out.println("分配任务给: " + machine.getMachineId());
                    machine.setOperationStatus("作业中");
                    break;
                }
            }
        } else if ("喷药".equals(taskType)) {
            for (AgriculturalMachine machine : machines) {
                if (machine instanceof agricultural.Sprayer) {
                    System.out.println("分配任务给: " + machine.getMachineId());
                    machine.setOperationStatus("作业中");
                    break;
                }
            }
        }

        // 更新导航信息
        navigationSystem.updateLocation(area);
        navigationSystem.updateHeading(0.0);
        navigationSystem.updateSpeed(5.0);

        System.out.println("任务分配完成，当前导航信息: " + navigationSystem.getNavigationInfo());
    }

    public void manageTask(String task, String action) {
        if ("完成".equals(action)) {
            if (tasks.contains(task)) {
                tasks.remove(task);
                System.out.println("任务 " + task + " 已完成");
            } else {
                System.out.println("任务 " + task + " 不存在");
            }
        } else if ("取消".equals(action)) {
            if (tasks.contains(task)) {
                tasks.remove(task);
                System.out.println("任务 " + task + " 已取消");
            } else {
                System.out.println("任务 " + task + " 不存在");
            }
        }
    }
}